package edu.cmu.cs.stage3.alice.authoringtool.viewcontroller;

import edu.cmu.cs.stage3.alice.authoringtool.AuthoringToolResources;

/**
 * @author Jason Pratt
 */
public class BooleanPropertyViewController extends PropertyViewController {
	protected javax.swing.JCheckBox checkBox = new javax.swing.JCheckBox();
	protected javax.swing.JLabel booleanLabel = new javax.swing.JLabel();
	protected java.awt.Color originalForegroundColor;

	public BooleanPropertyViewController() {
		originalForegroundColor = booleanLabel.getForeground(); // a bit of a hack; default color should be defined somewhere

		checkBox.setOpaque( false );
		checkBox.addActionListener(
				new java.awt.event.ActionListener() {
					public void actionPerformed( java.awt.event.ActionEvent ev ) {
						((Runnable)factory.createItem( BooleanPropertyViewController.this.checkBox.isSelected() ? Boolean.TRUE : Boolean.FALSE )).run();
					}
				}
		);
	}

	public void set( edu.cmu.cs.stage3.alice.core.Property property, boolean includeDefaults, boolean allowExpressions, boolean omitPropertyName, final edu.cmu.cs.stage3.alice.authoringtool.util.PopupItemFactory factory ) {
		super.set( property, includeDefaults, allowExpressions, false, omitPropertyName, factory );
		if( omitPropertyName ) {
			add( javax.swing.Box.createHorizontalStrut( 8 ), java.awt.BorderLayout.WEST );
		}

		refreshGUI();
	}

	//Rem @Override
	public void setEditingEnabled( boolean editingEnabled ) {
		super.setEditingEnabled( editingEnabled );
		checkBox.setEnabled( editingEnabled );
	}

	//Rem @Override
	protected java.awt.Component getNativeComponent() {
		return booleanLabel;
	}

	//Rem @Override
	protected Class getNativeClass() {
		return Boolean.class;
	}

	//Rem @Override
	protected void updateNativeComponent() {
		if( (property.getOwner() instanceof edu.cmu.cs.stage3.alice.core.response.IfElseInOrder) ||
				(property.getOwner() instanceof edu.cmu.cs.stage3.alice.core.question.userdefined.IfElse) ||
				(property.getOwner() instanceof edu.cmu.cs.stage3.alice.core.question.And) ||
				(property.getOwner() instanceof edu.cmu.cs.stage3.alice.core.question.Or) ||
				(property.getOwner() instanceof edu.cmu.cs.stage3.alice.core.question.Not) )
		{ // hack for red, unfinished look
		} else {
			booleanLabel.setForeground( originalForegroundColor ); // a bit of a hack; default color should be defined somewhere
		}
		booleanLabel.setText( AuthoringToolResources.getReprForValue( property.getValue(), property, property.getOwner().data ) );
	}
}
